package com.qboot.common.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.FieldNameConstants;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 全局响应结果
 *
 * @author Tellsea
 * @date 2022/11/21
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@FieldNameConstants
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private int code;

    private String msg;

    private T data;

    public static <T> Result<T> ok(String msg) {
        return new Result<T>(HttpStatus.OK.value(), msg, null);
    }

    public static <T> Result<T> ok(String msg, T data) {
        return new Result<T>(HttpStatus.OK.value(), msg, data);
    }

    public static <T> Result<T> ok(int code, String msg, T data) {
        return new Result<T>(code, msg, data);
    }

    public static <T> Result<T> fail(int code, String msg) {
        return new Result<T>(code, msg, null);
    }

    public static <T> Result<T> fail(String msg) {
        return new Result<T>(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, null);
    }

    public static <T> Result<T> fail(String msg, T data) {
        return new Result<T>(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, data);
    }

    public static <T> Result<T> fail(int code, String msg, T data) {
        return new Result<T>(code, msg, data);
    }

}
